package com.example.leaderapplication.Widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.leaderapplication.Bean.FindShipListResultBean;
import com.example.leaderapplication.Bean.FindShipRouteListResultBean;
import com.example.leaderapplication.R;

import java.util.List;

public class CustomShipListShowWindow extends PopupWindow {
    private static final String TAG = "CustomPopWindow";
    private final View view;
    private Activity context;

    private BaseQuickAdapter<FindShipListResultBean.DataBean, BaseViewHolder> adapter;
    private List<FindShipListResultBean.DataBean> data;

    public CustomShipListShowWindow(Activity context, List<FindShipListResultBean.DataBean> data, BaseQuickAdapter.OnItemClickListener itemClick) {
        super(context);
        this.data = data;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.widget_popupwindow, null);//alt+ctrl+f
        adapter = new BaseQuickAdapter<FindShipListResultBean.DataBean, BaseViewHolder>(R.layout.widget_popupwindow_item) {
            @Override
            protected void convert(BaseViewHolder helper, FindShipListResultBean.DataBean item) {
                helper.setText(R.id.popup_window_item_tv,item.getShipName());
            }
        };
        adapter.setOnItemClickListener(itemClick);
        this.context = context;
        initView();
        initPopWindow();
    }


    private void initView() {
        TextView cancelTv = view.findViewById(R.id.popup_cancel);
        RecyclerView rv = view.findViewById(R.id.popup_rv);
        rv.setLayoutManager(new LinearLayoutManager(context));
        rv.setAdapter(adapter);
//        for (int i = 0;i < 5;i++){
//            data.add("证件类型" + i);
//        }
        adapter.addData(data);
        adapter.notifyDataSetChanged();
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void initPopWindow() {
        this.setContentView(view);
        // 设置弹出窗体的宽
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // 设置弹出窗体可点击()
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.mypopwindow_anim_style);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x000000);
//        //设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        backgroundAlpha(context, 0.5f);//0.0-1.0
         this.backgroundAlpha(context, 0.5f);
    }

    /**
     * 设置添加屏幕的背景透明度(值越大,透明度越高)
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }
}
